package com.aiit.controller;

import com.aiit.pojo.User;
import com.aiit.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: LuTao
 * @Date: 2022/04/29/12:23
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/user_table")
    public String user_table(@RequestParam(value = "pn",defaultValue = "1") Integer pn,
                             Model model){
        List<User> list =userService.list();
        model.addAttribute("users",list);
        //分页查询数据
        Page<User> userPage = new Page<>(pn, 5);
        //分页查询的结果
        Page<User> page = userService.page(userPage, null);
        model.addAttribute("page",page);
        //当前页
        long current = page.getCurrent();
        model.addAttribute("pagecurrent",current);
        //数据库数据
        List<User> records = page.getRecords();
        model.addAttribute("pageuser",records);
        //分页总页数
        page.getPages();
        //总记录数
        long total = page.getTotal();

        return "/usertable/user_table02";
    }
    @GetMapping("user/delete/{id}")
    public String deleteUser(@PathVariable("id") Integer id,
                             @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                             RedirectAttributes ra){
        ra.addAttribute("pn",pn);
        userService.removeById(id);
        return "redirect:/user_table";
    }
    @RequestMapping(value = "useradd")
    public String register(User user){
        userService.register(user);
        return "redirect:/user_table";
    }
}
